package com.winter.service;

import com.google.common.base.Strings;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.IeliteRelainfoMapper;
import com.winter.mapper.NoticeCustRelaMapper;
import com.winter.model.IeliteRelainfo;
import com.winter.model.vo.request.CompanyItliteBean;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

@Service
public class IeliteRelainfoService {

    @Resource
    private IeliteRelainfoMapper ieliteRelainfoMapper;
    @Resource
    private NoticeCustRelaMapper noticeCustRelaMapper;

    /**
     * 添加收藏信息
     */
    public Result insertIelite(IeliteRelainfo info) {
        if (Strings.isNullOrEmpty(info.getCreateUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        info.setRelaType("0");
        info.setCreatetime(new Date());
        info.setUpdatetime(new Date());
        info.setUpdateUser(info.getCreateUser());
        int i = ieliteRelainfoMapper.insertSelective(info);
        if (i > 0) {
            return Result.success("新增收藏成功");
        } else {
            return Result.failure(ResultCode.INSERT_ERROR);
        }
    }

    /**
     * 取消收藏信息
     */
    public Result deleteIelite(String relaId, String userNo) {
    	 if (Strings.isNullOrEmpty(userNo)) {
             return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
         }
        if (null != relaId && null != userNo) {
            IeliteRelainfo a = ieliteRelainfoMapper.getRelainfoBean(userNo, relaId, "0");
            if (null != a) {
                ieliteRelainfoMapper.deleteByPrimaryKey(a.getId());
                return Result.success("取消收藏成功");
            } else {
                return Result.failure(ResultCode.INSERT_ERROR);
            }
        } else {
            return Result.failure(ResultCode.INSERT_ERROR);
        }
    }
    
    /*
     * 我的公司收藏列表
     * 
     */
    
    public Result findIeliteList(Integer pageSize, Integer pageNumber,String userNo,String type){
    	
    	List<CompanyItliteBean> itList = null;
    	if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
    	if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        if(null!=userNo&&null!=type){
        	itList=ieliteRelainfoMapper.findIeliteList(userNo, type);
        	
        }else{
        	 return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        PageInfo<CompanyItliteBean> pageInfo = new PageInfo<>(itList);
		return Result.success(pageInfo);
    	
    }
}
